package com.csii.pos.query;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.csii.adhoc.annotation.OperationLog;
import com.csii.adhoc.util.excel.ExcelUtils;
import com.csii.adhoc.util.myabtisplus.QueryWrapperUtils;
import com.csii.adhoc.controller.BaseController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csii.adhoc.util.ConvertUtils;
import com.csii.pos.query.service.ICashMntService;
import com.csii.pos.query.service.IExcelTemplateService;
import com.csii.pos.query.vo.CashMntVO;
import com.csii.result.APIResult;
import com.csii.result.PageVo;
import com.csii.result.TableHtmlBuilder;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Description: 头寸监测表
 * @Author: power-stdio
 * @Date: 2021-07-08
 * @Version: V1.0
 */

@RestController
@RequestMapping("/cashMnt")
@Slf4j
public class CashMntController extends BaseController<CashMnt, ICashMntService> {
    @Autowired
    private ICashMntService cashMntService;
    @Autowired
    private IExcelTemplateService excelTemplateService;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 分页列表查询
     *
     * @param cashMntVO 参数
     * @return 分页查询结果
     */
    @OperationLog(value = "头寸监测表-分页列表查询")
    @PostMapping(value = "/list")
    public APIResult<?> queryPageList(@RequestBody CashMntVO cashMntVO) {
        CashMnt cashMnt = ConvertUtils.convert(CashMnt.class, cashMntVO);
        QueryWrapper<CashMnt> queryWrapper = QueryWrapperUtils.getEqQueryWrapperFromDto(cashMnt, new QueryWrapper<>());
        Page<CashMnt> page = new Page<CashMnt>(cashMntVO.getPageNo(), cashMntVO.getPageSize());
        queryWrapper.orderByAsc("SEQ_NO");
        IPage<CashMnt> pageList = cashMntService.page(page, queryWrapper);
        String html = TableHtmlBuilder.buildTableHtml(pageList.getRecords());
        return APIResult.successResult(PageVo.getPageVoFromIPage(pageList)).setTableHtml(html);
    }

    /**
     * 添加
     *
     * @param cashMntVO 参数
     * @return 添加结果
     */
    @OperationLog(value = "头寸监测表-添加")
    @PostMapping(value = "/add")
    public APIResult<?> add(@RequestBody CashMntVO cashMntVO) {
        CashMnt cashMnt = ConvertUtils.convert(CashMnt.class, cashMntVO);

        cashMntService.save(cashMnt);
        return APIResult.successResult("添加成功！");
    }

    /**
     * 编辑
     *
     * @param cashMntVO 参数
     * @return 编辑结果
     */
    @OperationLog(value = "头寸监测表-编辑")
    @PostMapping(value = "/edit")
    public APIResult<?> edit(@RequestBody CashMntVO cashMntVO) {
        CashMnt cashMnt = ConvertUtils.convert(CashMnt.class, cashMntVO);
        cashMntService.updateById(cashMnt);
        return APIResult.successResult("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param map id
     * @return 删除结果
     */
    @OperationLog(value = "头寸监测表-通过id删除")
    @PostMapping(value = "/delete")
    public APIResult<?> delete(@RequestBody Map<String, String> map) {
        cashMntService.removeById(map.get("id"));
        return APIResult.successResult("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids id集合
     * @return 删除结果
     */
    @OperationLog(value = "头寸监测表-批量删除")
    @PostMapping(value = "/deleteBatch")
    public APIResult<?> deleteBatch(@RequestBody List<CashMntVO> ids) {
        this.cashMntService.removeByIds(ids.stream().map(CashMntVO::getId).collect(Collectors.toList()));
        return APIResult.successResult("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id id
     * @return 查询结果
     */
    @OperationLog(value = "头寸监测表-通过id查询")
    @PostMapping(value = "/queryById")
    public APIResult<?> queryById(@RequestParam(name = "id") String id) {
        CashMnt cashMnt = cashMntService.getById(id);
        if (cashMnt == null) {
            return APIResult.failResult(null, "未找到对应数据");
        }
        CashMntVO cashMntVO = ConvertUtils.convert(CashMntVO.class, cashMnt);
        return APIResult.successResult(cashMntVO);
    }

    /**
     * 导出excel
     *
     * @param cashMntVO 参数
     */
    @RequestMapping(value = "/exportXls")
    public void exportXls(HttpServletResponse response, CashMntVO cashMntVO) {
        QueryWrapper<ExcelTemplate> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("TABLE_NAME", "CASH_MNT");
        ExcelTemplate template = excelTemplateService.getOne(queryWrapper);
        ExcelUtils.exportTemplate(template.getTemplateName(), template.getTemplateDir(), cashMntVO.getCashMntDt(), jdbcTemplate, response);
    }

    /**
     * 通过excel导入数据
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public APIResult<?> importExcel(@RequestParam("file") MultipartFile file) throws Exception {
        return APIResult.successResult(super.importExcel(file, CashMnt.class));
    }
}

